import java.util.HashMap;
import java.util.Map;

/**
 * Created by zybang on 2017/6/27.
 * 判断两个字符串是否是置换关系
 */
public class stringPermutation {
    public static void main(String[] args){
        System.out.print(solution("abc","cba"));
    }

    private static boolean solution(String A, String B){
        if(A.equals(B) ) return true;
        if(null == A || null == B || A.length() != B.length()) return false;
        if(A.equals(B)) return true;
        Map<Character, Integer> map = new HashMap<Character, Integer>();
        for(int i = 0; i < A.length(); i++) {
            char a = A.charAt(i);
            if(map.containsKey(a)) {
                if(map.get(a)+1 == 0) {
                    map.remove(a);
                }else{
                    map.put(a, map.get(a)+1);
                }
            }else {
                map.put(a, 1);
            }

            char b = B.charAt(i);
            if(map.containsKey(b)) {
                if(map.get(b)-1 == 0) {
                    map.remove(b);
                }else{
                    map.put(b, map.get(b)-1);
                }
            }else {
                return false;
            }
        }
        if(map.size() == 0)
            return true;
        return false;

    }
}
